package com.baiyang.app.ui.read;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.MyApp;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.DataTool;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class ReadPageService {

    private Context context;
    private ReadPageViewModel viewModel;
    public ReadPageService(Context context,ReadPageViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }

    public void getContent(String articleId){
        JSONObject object = new JSONObject();
        object.put("articleId",articleId);
        User user = MyApp.getInstance().getUser();
        object.put("userId",user.getId());
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("read",str);
                    viewModel.setContentResult(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("read","code:" + response.code());
                }
            }
            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("read","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.READ_RECORD.READ,object);
    }

    /**
     * 获取关注状态
     */
    public void getFollowState(String articleId){
        JSONObject object = new JSONObject();
        object.put("articleId",articleId);
        User user = MyApp.getInstance().getUser();
        object.put("userId",user.getId());
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("getFollowState",str);
                    viewModel.setFollowStateResult(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("getFollowState","code:" + response.code());
                }
            }
            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("getFollowState","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.BOOKSHELF.GET_FOLLOW_STATE,object);
    }

    /**
     * 关注
     */
    public void follow(String articleId){
        JSONObject object = new JSONObject();
        object.put("articleId",articleId);
        User user = MyApp.getInstance().getUser();
        object.put("userId",user.getId());
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("follow",str);
                    viewModel.setFollowResult(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("follow","code:" + response.code());
                }
            }
            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("follow","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.BOOKSHELF.FOLLOW,object);
    }

}
